<template>
  <el-dialog :visible.sync="dialog" title="学员退费" append-to-body width="570px">
    <el-form ref="refundForm" :model="refundForm" :rules="rules" size="small" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="姓名:" prop="name">{{ name }}</el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="退费金额:" prop="amount">
            <el-input v-model="refundForm.amount" placeholder="请输入退费金额"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="元"></el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="退费原因:" prop="reason">
            <el-input
              v-model="refundForm.reason"
              :autosize="{ minRows: 3}"
              type="textarea"
              placeholder="请输入退费原因"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { refund } from '@/api/student'
export default {
  props: {
    merchantStudentId: {
      type: [String, Number],
      required: true
    },
    name: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      dialog: false,
      loading: false,
      refundForm: {
        merchantStudentId: null,
        amount: 0,
        reason: ''
      },
      rules: {
        amount: [
          { required: true, message: '请输入退款金额', trigger: 'blur' }
        ],
        reason: [
          { required: true, message: '请输入退款原因', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.refundForm.merchantStudentId = this.merchantStudentId
      this.$refs['refundForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          refund(this.refundForm).then(res => {
            this.$notify({
              title: '退款申请成功，等待订单审核',
              type: 'success',
              duration: 2500
            })
            this.loading = false
            this.dialog = false
          }).catch(err => {
            this.$message.error('退款申请失败，' + err.msg)
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['refundForm'].resetFields()
    }
  }
}
</script>
